const moment = require('moment');
const crypto = require('crypto');

module.exports = {

    // --------------------------正则部分--------------------------

    //是否手机号
    isMobile(v) {
        const re = /^(?:13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])-?\d{5}(\d{3}|\*{3})$/;
        return re.test(v);
    },
    //是否邮箱
    isEmail(v) {
        const re = /^([a-zA-Z0-9]+\.?[a-zA-Z0-9]+)+@[a-zA-Z0-9]+(\.[a-zA-Z0-9]{2,})$/;
        return re.test(v);
    },
    //是否包含空格
    isTrim(v) {
        return v.indexOf(' ') !== -1;
    },
    //是否包含数字
    isContainNumber(v) {
        const re = /[0-9]/;
        return re.test(v);
    },
    //是否包含字母
    isContainAlpha(v) {
        const re = /[a-z]/i;
        return re.test(v);
    },
    //是否包含中文字
    isContainChinese(v) {
        const re = /[\u4e00-\u9fa5]/;
        return re.test(v);
    },
    //是否是Array
    isArray(v) {
        return Object.prototype.toString.call(v) === '[object Array]';
    },
    //是否是String
    isString(v) {
        return Object.prototype.toString.call(v) === '[object String]';
    },
    //是否是Boolean
    isBoolean(v) {
        return Object.prototype.toString.call(v) === '[object Boolean]';
    },
    //是否是Undefined
    isUndefined(v) {
        return Object.prototype.toString.call(v) === '[object Undefined]';
    },
    //是否是Null
    isNull(v) {
        return Object.prototype.toString.call(v) === '[object Null]';
    },
    //是否是Object
    isObject(v) {
        return Object.prototype.toString.call(v) === '[object Object]';
    },
    //是否是Function
    isFunction(v) {
        return Object.prototype.toString.call(v) === '[object Function]';
    },
    //是否是Number
    isNumber(v) {
        return Object.prototype.toString.call(v) === '[object Number]';
    },
    //域名验证 中文\u4e00-\u9fa5
    isDomainName(str) {
        const strRegex = /^((https|http):\/\/)?((localhost)|(([0-9]{1,3}\.){3}[0-9]{1,3})|(([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,}))(\/\w+)*((\.[a-zA-Z]+)|\/)*$/;
        return strRegex.test(str);
    },
    //是否是httpUrl
    isUrl(str) {
        const strRegex = /^((https|http):\/\/)?((localhost)|(([0-9]{1,3}\.){3}[0-9]{1,3})|(([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,}))/;
        return strRegex.test(str);
    },
    //整数验证
    isParseInt(n) {
        const strRegex = /^\-?[0-9]+$/;
        return strRegex.test(n);
    },
    //正整数验证
    isZParseInt(n) {
        const strRegex = /^[0-9]+$/;
        return strRegex.test(n);
    },
    //整数日期
    isDate(n) {
        const strRegex = /^(\d{4})-(\d{2})-(\d{2})$/;
        return strRegex.test(n);
    },
    //路径验证
    isPath(n) {
        const strRegex = /^\S+\.[a-zA-Z]+$/;
        return strRegex.test(n);
    },
    //端口验证
    isPort(n) {
        const strRegex = /^[0-9]{1,5}$/;
        return strRegex.test(n);
    },



    // --------------------------功能部分--------------------------

    //删除空格
    deleteSpace(str) {
        if (typeof str != 'string')
            return str;
        str = str.replace(/(^\s*)|(\s*$)/g, "");
        return str;
    },
    //去掉http/https
    removeHttpStr(str) {
        if (typeof str != 'string')
            return str;
        str = str.replace(/^(http:\/\/|https:\/\/)/g, "");
        return str;
    },
    //添加http/https
    addHttpStr(str) {
        if (typeof str != 'string')
            return str;
        const strRegex = /^(http|https)/g;
        if (strRegex.test(str))
            return str;
        else
            return 'http://' + str;
    },
    //对象值排序组成字符串
    objSortToString(o) {
        const _ary = [];
        for (var _p in o) {
            _ary.push(o[_p]);
        };

        const _newAry = _ary.sort();
        const string = _newAry.join('');
        return string;
    },
    // 生成随机字符串
    createNonceStr(length) {
        length = length || 15;
        length += 2;
        return Math.random().toString(36).substr(2, length);
    },
    // 生成随机数
    createNonceNum(length) {
        length = length || 15;
        length += 2;
        return Math.random().toString().substr(2, length);
    },
    //柜台转换成json数据
    counterToJson(data, level = 3) {
        let Data = [],
            _item, list1 = {},
            list2 = {},
            _province, _city;
        if (!this.isArray(data))
            return Data;
        for (let n = 0, lgt = data.length; n < lgt; n++) {
            _item = data[n];
            switch (level) {
                case 2:
                    _city = _item.City;
                    if (list2.name != _city) {
                        list2 = {
                            name: _city,
                            list: []
                        };
                        Data.push(list2);
                    }
                    break;

                case 3:
                    _province = _item.Province;
                    _city = _item.City;
                    if (list1.name != _province) {
                        list1 = {
                            name: _province,
                            list: []
                        };
                        Data.push(list1);
                    }
                    if (list2.name != _city) {
                        list2 = {
                            name: _city,
                            list: []
                        };
                        list1.list.push(list2);
                    }
                    break;

            }

            delete _item.Province;
            delete _item.City;
            list2.list.push(_item);
        }
        return Data;
    },
    // 生成时间戳
    get createTimestamp() {
        return parseInt(new Date().getTime() / 1000) + '';
    },
    //时间为基础随机ID
    get getNonDuplicateID() {
        return Number(Math.random().toString() + Date.now()).toString(36).substr(2, 15);
    },
    //当前时间
    get dateNow() {
        return moment().format('YYYY-MM-DD HH:mm:ss');
    },
    //utc服务器加8小时
    get dateUtcNow() {
        return moment().add(8, 'h').format('YYYY-MM-DD HH:mm:ss');
    },
    //显示日期格式
    formatDate(date) {
        return moment(date).format('YYYY-MM-DD HH:mm:ss');
    },
    //加密方法
    encrypt: function (key, iv, data) {
        var cipher = crypto.createCipheriv('aes-128-cbc', key, iv);
        var crypted = cipher.update(data, 'utf8', 'binary');
        crypted += cipher.final('binary');
        crypted = new Buffer(crypted, 'binary').toString('base64');
        return crypted;
    },
    //解密方法
    decrypt: function (key, iv, crypted) {
        crypted = new Buffer(crypted, 'base64').toString('binary');
        var decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
        var decoded = decipher.update(crypted, 'binary', 'utf8');
        decoded += decipher.final('utf8');
        return decoded;
    },

}